/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.sciStudio.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.sciStudio.domain.SciStudio;
import com.fzhucc.modules.sciStudio.domain.dto.*;
import com.fzhucc.modules.sciStudio.service.SciStudioService;
import com.fzhucc.modules.sciTechnologyDemand.domain.dto.*;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2024-01-10
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "工作室:管理")
@RequestMapping("/api/sciStudio")
public class SciStudioController {

    private final SciStudioService sciStudioService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sciStudio:list')")
    public void exportSciStudio(HttpServletResponse response, SciStudioQueryCriteria criteria) throws IOException {
        sciStudioService.download(sciStudioService.queryAll(criteria), response);
    }

    @Log("我的导出数据")
    @ApiOperation("我的导出数据")
    @GetMapping(value = "/my/download")
    @PreAuthorize("@el.check('sciStudio:list')")
    public void myexportSciTechnologyDemand(HttpServletResponse response, SciStudioQueryCriteria criteria) throws IOException {
        sciStudioService.download(sciStudioService.queryAllmy(criteria), response);
    }

    @GetMapping
    @Log("查询工作室")
    @ApiOperation("查询工作室")
    @PreAuthorize("@el.check('sciStudio:list')")
    public ResponseEntity<Object> querySciStudio(SciStudioQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(sciStudioService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/my")
    @Log("查询我的工作室")
    @ApiOperation("查询我的工作室")
    public ResponseEntity<Object> my(SciStudioQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(sciStudioService.my(criteria, pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/list")
    @Log("h5、小程序查询工作室")
    @ApiOperation("h5、小程序查询工作室")
    public ResponseEntity<Object> list(SciStudioQueryCriteria criteria, Pageable pageable) {
        criteria.setIsShow(1);
        return new ResponseEntity<>(sciStudioService.queryAllh5(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工作室")
    @ApiOperation("新增工作室")
    //@PreAuthorize("@el.check('sciStudio:add')")
    public ResponseEntity<Object> createSciStudio(@Validated @RequestBody SciStudioAddDto dto){
        return new ResponseEntity<>(sciStudioService.create(dto), HttpStatus.CREATED);
    }

    @GetMapping(value = "/getStudioType")
    @Log("获取字典工作室类型")
    @ApiOperation("获取字典工作室类型")
    @AnonymousAccess
    public ResponseEntity<Object> getStudioType() {
        return new ResponseEntity<>(sciStudioService.getStudioType(), HttpStatus.OK);
    }

    @PutMapping
    @Log("修改工作室")
    @ApiOperation("修改工作室")
    @PreAuthorize("@el.check('sciStudio:edit')")
    public ResponseEntity<Object> updateSciStudio(@Validated @RequestBody SciStudioEditDto resources){
        sciStudioService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工作室")
    @ApiOperation("删除工作室")
    @PreAuthorize("@el.check('sciStudio:del')")
    public ResponseEntity<Object> deleteSciStudio(@RequestBody Integer[] ids) {
        sciStudioService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping(value = "/isAllowEdit")
    @Log("是否允许编辑")
    @ApiOperation("是否允许编辑")
    public ResponseEntity<Object> isAllowEdit(@Validated @RequestBody AllowSciStudioDto dto) {
        sciStudioService.isAllowEdit(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "/audit")
    @Log("工作室状态审核")
    @ApiOperation("工作室状态审核")
    public ResponseEntity<Object> auditSciStudio(@Validated @RequestBody AuditSciStudioDto dto) {
        sciStudioService.auditSciStudio(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个工作室")
    @ApiOperation("查询单个工作室")
    public SciStudioDto queryInfoById(@PathVariable Integer id) {
        return sciStudioService.findById(id);
    }
}